var fs = require('fs')
function repla(path = '') {
  const index = path.indexOf(':')
  if (index > -1) {
    return path.slice(0, index - 1)
  }
  return path
}
function handelRouters(data = [], parent = {}, list = [], init) {
  // eslint-disable-next-line no-eval
  const data1 = eval(data)
  data1.map(i => {
    i.name = (parent.name ? parent.name + '/' : '') + i.name
    i.path = (parent.path ? repla(parent.path) + '/' : '') + repla(i.path)
    if (i.children && i.children.length > 0) {
      if (!init) {
        list.push({
          name: i.name,
          path: repla(i.path) + '/index',
          template: 'routerView'
        })
      }
      list.push(...handelRouters(i.children, i))
    } else {
      list.push({
        name: i.name,
        path: repla(i.path),
        template: i.template
      })
    }
  })
  return list
}
var routerArr = fs.readFileSync('src/router/actionRouter.js', 'utf8')
const data = routerArr.split('list =')[1].split('export default')[0].replace(/Layout/g, '1')
const routers = handelRouters(data, {}, [], 'init')
module.exports = {
  description: 'generate a view',
  prompts: [],
  actions: data => {
    const list = routers.map(i => {
      let path = i.path
      const splitArr = path.split('/')
      if (splitArr.length === 2 && splitArr[0] === '') {
        path += '/index'
      }
      return {
        abortOnFail: false,
        skipIfExists: true,
        type: 'add',
        path: `src/views${path}.vue`,
        templateFile: `plop-templates/view/${i.template || 'ag'}.hbs`
      }
    })
    console.log(list)
    const actions = [
      ...list
    ]

    return actions
  }
}
